﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace RandomStudent.Utilities
{
    public static class Global
    {
        /// <summary>
        /// 装拆箱复制对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <returns></returns>
        public static T CloneObject<T>(this T o)
        {
            T t = Activator.CreateInstance<T>();
            var propertyInfos = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (var propertyInfo in propertyInfos)
            {
                Type type = propertyInfo.GetType();
                var value = propertyInfo.GetValue(o, null);
                propertyInfo.SetValue(t, value);
            }
            return t;
        }
    }
}
